package com.tiger.web.controller.statistics;

import com.tiger.common.core.domain.AjaxResult;
import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.utils.ServletUtils;
import com.tiger.common.utils.spring.SpringUtils;
import com.tiger.framework.web.service.TokenService;
import com.tiger.project.service.IProjectStatisticsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 项目统计相关API请求控制类
 *
 * @author Lyn 宋开放
 * @date 2023-01-09 10:09
 **/
@RestController
@RequestMapping("statistics/project")
public class ProjectStatisticsController {

    private final IProjectStatisticsService projectStatisticsService;

    public ProjectStatisticsController(IProjectStatisticsService projectStatisticsService) {
        this.projectStatisticsService = projectStatisticsService;
    }

    /**
     * 查询近12个月巡查记录
     * @return
     */
    @GetMapping("workplace/patrol")
    public AjaxResult patrolStatisticsLastYear(@RequestParam("companyId") long companyId){
        return AjaxResult.success(projectStatisticsService.getPatrolStatisticsLastYear(companyId));
    }

    /**
     * 查询项目统计数据
     * @return
     */
    @GetMapping("workplace/projectTotal")
    public AjaxResult projectTotalStatistics(@RequestParam("companyId") long companyId){
        return AjaxResult.success(projectStatisticsService.getProjectTotalStatistics(companyId));
    }


    /**
     * 根据年份获取当年12个月的维修申请统计数据
     * @param year 需要查询的年份
     * @return
     */
    @GetMapping("workplace/maintenanceStatistics")
    public AjaxResult maintenanceStatistics(String year, @RequestParam("companyId") long companyId){
        return AjaxResult.success(this.projectStatisticsService.getMaintenanceStatisticsByYear(year, companyId));
    }


    /**
     * 获取各年份维修数据统计
     * @return
     */
    @GetMapping("workplace/maintenanceSummary")
    public AjaxResult maintenanceSummary(@RequestParam("companyId") long companyId){
        return AjaxResult.success(this.projectStatisticsService.getMaintenanceSummary(companyId));
    }

    @GetMapping("maintenanceStatistics")
    public AjaxResult maintenanceLast12MonthStatistics(@RequestParam("companyId") long companyId){
        return AjaxResult.success(this.projectStatisticsService.getMaintenanceLast12MonthStatistics(companyId));
    }

    @GetMapping("totalSummary")
    public AjaxResult totalSummary(){
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return AjaxResult.success(this.projectStatisticsService.getMaintenanceTotalSummary(loginUser.getUser().getDeptId()));
    }
}
